package danceserver;

import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import processing.core.*;

public class View {

    PApplet parent;
    float rotation;
    int count;
    PImage sunimg;
    Frame _frame;
    JProgressBar xJProgressBar;
    JProgressBar yJProgressBar;
    JProgressBar zJProgressBar;
    JLabel xLabel;
    JLabel yLabel;
    JLabel zLabel;
    JCheckBox xButton;
    JCheckBox yButton;
    JCheckBox zButton;
    Controller _controller;
    int lastXReading = 0;
    int lastYReading = 0;
    int lastZReading = 0;
    int xReading = 0;
    int yReading = 0;
    int zReading = 0;
    int sphereX = 300;
    int sphereY = 300;
    int sphereZ = -120;

    public View(PApplet p, Controller c) {
        parent = p;
        rotation = 1;
        count = 0;
        sunimg = parent.loadImage("sun5.jpg");
        _controller = c;
        parent.sphereDetail(180);
        createFrame();
        xReading = 100;
        yReading = 100;
        zReading = 100;
    }

    private void drawCube() {
        // Draw Cube
        parent.background(0);
        parent.lights();
        parent.noStroke();
        parent.pushMatrix();
        parent.translate(130, parent.height / 2, 0);
        parent.rotateY((float) (xReading * .1));
        parent.rotateX((float) (yReading * .1));
        parent.box(zReading);
        parent.popMatrix();
    }

    private void drawSun() {
        // Draw sunButton
        parent.background(0);
        parent.lights();
        parent.translate(parent.width / 2, parent.height / 2);
        parent.rotateY(PApplet.map(parent.mouseX, 0, parent.width, -PApplet.PI, PApplet.PI));
        parent.rotateX(PApplet.map(parent.mouseY, 0, parent.height, -PApplet.PI, PApplet.PI));
        parent.rotateZ(PApplet.PI / 6);
        parent.beginShape();
        parent.texture(sunimg);
        parent.vertex(-100, -100, 0, 0, 0);
        parent.vertex(100, -100, 0, 400, 0);
        parent.vertex(100, 100, 0, 400, 400);
        parent.vertex(-100, 100, 0, 0, 400);
        parent.endShape();
    }

    private void drawSphere() {
        // Draw View
        parent.background(0);
        parent.lights();
        parent.fill(204, 102, 0);
        parent.stroke(204, 102, 0);
        parent.pushMatrix();
        parent.translate(sphereX, sphereY, sphereZ);
        parent.sphere(100);
        parent.popMatrix();
        // Orange point light on the right
        parent.pointLight(150, 100, 0, // Color
                200, -150, 0); // Position
    }

    private void drawLights() {

        parent.background(0);
        //parent.lights();
        parent.fill(153);
        parent.translate(parent.width / 2, parent.height / 2);

        // Orange point light on the right
        parent.pointLight(150, 100, 0, // Color
                200, -150, 0); // Position

        // Blue directional light from the left
        parent.directionalLight(0, 102, 255, // Color
                1, 0, 0); // The xJProgressBar-, yJProgressBar-, zJProgressBar-axis direction

        // Yellow spotlight from the front
        parent.spotLight(255f, 255f, 109f, // Color
                0f, 40f, 200f, // Position
                0f, -0.5f, -0.5f, // Direction
                PApplet.PI / 2, 2); // Angle, concentration

        parent.rotateY(PApplet.map(parent.mouseX, 0, parent.width, 0, PApplet.PI));
        parent.rotateX(PApplet.map(parent.mouseY, 0, parent.height, 0, PApplet.PI));
        parent.box(150);
    }

    public void draw(VisType _type) {

        switch (_type) {

            case CUBES:
                drawCube();
                break;
            case SUN:
                drawSun();
                break;
            case SPHERE:
                drawSphere();
                break;
            case LIGHTS:
                drawLights();
                break;
            case OFF:
                break;
        }

    }

    public void createFrame() {
        System.out.println("creating frame");
        Button gModeButton;
        Button exitButton;
        Button cubesButton;
        Button sunButton;
        Button sphereButton;
        Button lightsButton;



        xJProgressBar = new JProgressBar(SwingConstants.VERTICAL, 0, 100);
        xJProgressBar.setValue(500);

        yJProgressBar = new JProgressBar(SwingConstants.VERTICAL, 0, 100);
        yJProgressBar.setValue(500);

        zJProgressBar = new JProgressBar(SwingConstants.VERTICAL, 0, 100);
        zJProgressBar.setValue(500);


        _frame = new Frame("Java Applet");
        _frame.setLocation(75, 75);
        _frame.setLayout(new GridLayout(2, 6));


        _frame.add(gModeButton = new Button("2g/6g"));
        _frame.add(exitButton = new Button("Exit"));
        _frame.add(cubesButton = new Button("CUBES"));
        _frame.add(sunButton = new Button("SUN"));
        _frame.add(sphereButton = new Button("SPHERE"));
        _frame.add(lightsButton = new Button("LIGHTS"));
        _frame.add(xButton = new JCheckBox("X on"));
        _frame.add(yButton = new JCheckBox("Y on"));
        _frame.add(zButton = new JCheckBox("Z on"));

        _frame.add(xLabel = new JLabel("   X:"));
        _frame.add(xJProgressBar);
        _frame.add(yLabel = new JLabel("   Y:"));
        _frame.add(yJProgressBar);

        _frame.add(zLabel = new JLabel("   Z:"));
        _frame.add(zJProgressBar);

        gModeButton.addActionListener(_controller);
        exitButton.addActionListener(_controller);
        cubesButton.addActionListener(_controller);
        sunButton.addActionListener(_controller);
        sphereButton.addActionListener(_controller);
        lightsButton.addActionListener(_controller);


        _frame.pack();
        _frame.setVisible(true);

    }

    public void setSphereX(int x) {
        this.sphereX = this.sphereX + ((100 - x) - 55);
        if (this.sphereX < 0) {
            this.sphereX = 0;
        }
        if (this.sphereX > 600) {
            this.sphereX = 600;
        }

    }

    public void setSphereY(int y) {
        this.sphereY = this.sphereY + (y - 40);
        if (this.sphereY < 0) {
            this.sphereY = 0;
        }
        if (this.sphereY > 600) {
            this.sphereY = 600;
        }

    }

    public void setSphereZ(int z) {
        this.sphereZ = this.sphereZ + (z - 50);
        if (this.sphereZ < -200) {
            this.sphereZ = -200;
        }
        if (this.sphereZ > 0) {
            this.sphereZ = 0;
        }
    }

    public int getxReading() {
        return xReading;
    }

    public int getyReading() {
        return yReading;
    }

    public int getzReading() {
        return zReading;
    }

    public void setxReading(int xReading) {
        if (xButton.isSelected()) {
            this.lastXReading = xReading;
            this.xReading = xReading;
            this.setSphereX(xReading);
        }
    }

    public void setyReading(int yReading) {
        if (yButton.isSelected()) {
            this.lastYReading = yReading;
            this.yReading = yReading;
            this.setSphereY(yReading);
        }
    }

    public void setzReading(int zReading) {
        if (zButton.isSelected()) {
            this.lastZReading = zReading;
            this.zReading = zReading;
            this.setSphereZ(zReading);
        }
    }
}
